// pages/SeiyHaiLab/btnPage/btnPage.js
const Audio = wx.createInnerAudioContext();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    duration: '', //歌曲长度
    currentTime: '', //当前播放时间
    songTitle: '', //歌名
    songPath: 'http://music.163.com/song/media/outer/url?id=1340519788', //歌曲路径
    songImg: '', //歌曲封面
    singer: '', //歌手
    songArray: [], //存储歌曲信息的数组
    rotated: 'rotated', //实现封面旋转的标志
    isActive: true //实现暂停播放，上一曲，下一曲，及封面转动的标志
  },
  gotSong: function () { //获取歌曲信息
    var that = this
    wx.request({ //微信的request请求
      url: 'https://api.uomg.com/api/rand.music?format=json',
      success: function (res) {
        that.data.songArray = that.data.songArray.concat(res.data.data) //拼接返回的信息为一个数组
        wx.setStorageSync('songArray', that.data.songArray) //设置本地缓存
        // Audio.src = res.data.data.url //设置歌曲路径
        Audio.src = 'http://music.163.com/song/media/outer/url?id=28068737' //原来在这
        that.setData({ //设置信息
          // songTitle: res.data.data.name,
          songTitle: "걱정말아요 그대",
          // songPath: res.data.data.url,
          // songImg: res.data.data.picurl,
          songImg: 'http://p2.music.126.net/zPpMOPCZVuYDlLnD25o_UA==/109951163658947777.jpg?param=130y130',
          // singer: res.data.data.artistsname
          singer: "野菊花"
        })
        // console.log(res.data.data)
      }
    })
  },
  //上一首
  pre: function (e) {
    var that = this
    var isActive = that.data.isActive //获取标志
    if (!isActive) {
      that.setData({
        isActive: true
      })
    }
    var len = that.data.songArray.length - 2 //通过调整数组实现上一曲 数组是零序的 数组长度1序 所以减2位上一首歌位置所在序号
    var array = that.data.songArray
    if (len >= 0) { //大于等于零就是刚开始一首歌，没有上一曲
      Audio.src = array[len].url
      that.setData({
        songTitle: array[len].name,
        // songPath: array[len].url,
        // songPath: "http://music.163.com/song/media/outer/url?id=1340519788",
        songImg: array[len].picurl,
        singer: array[len].artistsname
      })
      that.data.songArray.pop()
    }
  },
  //下一首
  next: function () {
    var that = this
    var isActive = that.data.isActive
    if (!isActive) {
      that.setData({
        isActive: true
      })
    }
    wx.request({
      url: 'https://api.uomg.com/api/rand.music?format=json',
      success: function (res) {
        that.data.songArray = that.data.songArray.concat(res.data.data)
        wx.setStorageSync('songArray', that.data.songArray)
        Audio.src = res.data.data.url
        that.setData({
          songTitle: res.data.data.name,
          // songPath: res.data.data.url,
          songImg: res.data.data.picurl,
          singer: res.data.data.artistsname,
        })
        // console.log(res.data.data)
      }
    })
  },
  // 暂停或播放
  play: function () {
    var that = this
    var isActive = that.data.isActive
    var rotated = that.data.rorated
    if (isActive) {
      Audio.pause();
      rotated = ''
      that.setData({
        isActive: false,
        rotated: rotated
      })
      // console.log('暂停了');
    } else {
      rotated: rotated
      that.setData({
        isActive: true,
        rotated: 'rotated'
      })
      Audio.play();
      // console.log('播放了');
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.gotSong()
    Audio.autoplay = false //设置自动播放
    Audio.onPlay(() => { //监测播放事件
      console.log('开始自动播放')
    })
    Audio.onError((res) => { //监测播放失败事件
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    Audio.onEnded(() => { //监听歌曲自然播放结束
      that.gotSong(); //调用gotsong方法，播放下一首歌
      var isActive = that.data.isActive //设置图标
      if (!isActive) {
        that.setData({
          isActive: true
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})